#ifndef _VOTE_KNN_SIM_WEIGHT_H
#define _VOTE_KNN_SIM_WEIGHT_H

#include "VoteKNN.h"

#include <algorithm>
#include <map>
#include <iostream>

using namespace std;


class VoteKNNSimWeight
    : public VoteKNN
{
public:

    VoteKNNSimWeight(){}

    ~VoteKNNSimWeight(){}

    static STRING insName()
    {
        return "SimWeight";
    }

    W_VEC vote(IN PriorityQueue& _pq, IN STRING _simName)
    {
        W_VEC ret;
        typedef map<DOUBLE, DOUBLE> MAP;
        MAP count;
        count.clear();

        for (UINT32 i = 0; i < _pq.size(); ++i)
        {
            DOUBLE lbl = _pq[i].label;
            DOUBLE c = 0;
            if (count.find(lbl) != count.end())
            {
                c = count[lbl];
            }
            count[lbl] = c + _pq[i].sim;
        }

        DOUBLE total = 0;
        for (MAP::iterator ite = count.begin(); ite != count.end(); ++ite)
        {
            total += (*ite).second;
        }

        for (MAP::iterator ite = count.begin(); ite != count.end(); ++ite)
        {
            Weight w;
            w.label = (*ite).first;
            w.weight = (*ite).second * 1.0 / total;
            ret.push_back(w);
        }

        sort(ret.begin(), ret.end());

        return ret;
    }

};

#endif // #ifndef _VOTE_KNN_SIM_WEIGHT_H